package pf.springBoot.springBootSSO.apiController;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pf.java.pfHelper.AbstractApiResult;
import pf.java.pfHelper.PFAllowAnonymous;
import pf.java.pfHelper.model.UserOrg;
import pf.java.pfHelper.model.UserTypeClass;
import pf.springBoot.springBootSSO.oAuth.FormsAuth;
import pf.springBoot.springBootSSO.oAuth.MetabaseDataHelper;
import pf.springBoot.springBootSSO.projHelper.*;

@RestController
//@RequestMapping("/User")
public class UserApiController {
	/*
	 * 当用户使用vpn时，要用ajax通过此方法判断是否能够连接sso服务，所以要CrossOrigin跨域
	 */
    @PFAllowAnonymous
  	@GetMapping(value = "/IsAlive")
    //@CrossOrigin(origins = "http://192.168.1.10:8080", maxAge = 3600)
    @CrossOrigin(origins = "*", maxAge = 3600)
    public Boolean IsAlive()
    {
        return true;
    }
    
	@PFAllowAnonymous
  	@PostMapping(value = "/User/ValidSSOToken")
    public AbstractApiResult<?> ValidSSOToken(@RequestParam String token) {
        return AbstractApiResult.success(JwtHelper.verifyToken(token, MetabaseDataHelper.METABASE_JWT_SHARED_SECRET).get("SysUser"));
    }
//
//	@PFAllowAnonymous
//  	@PostMapping(value = "/User/SSOLogout")
//    public AbstractApiResult<?> SSOLogout() {
//		String UserId=FormsAuth.GetUserData().UserCode;	
//        if (!PFDataHelper.StringIsNullOrWhiteSpace(UserId))
//        {
//            PFCaching.Remove(UserId);
//            FormsAuth.SingOut();
//            return AbstractApiResult.success();
//        }
//        else
//        {
//            return AbstractApiResult.error("未登陆");
//        }
//    }

    @PFAllowAnonymous
  	@GetMapping(value = "/User/IsAuthSSO")
    public Boolean IsAuthSSO()
    {
        return FormsAuth.IsLogined();
    }

    @PFAllowAnonymous
  	@GetMapping(value = "/UserOrg")
    //@CrossOrigin(origins = "http://192.168.1.10:8080", maxAge = 3600)
    @CrossOrigin(origins = "*", maxAge = 3600)
    public UserOrg UserOrg()
    {
    	UserOrg user=new UserOrg();
    	user.Org="02000";
    	user.UserType=UserTypeClass.Fgs;
    	return user;
    }
}
